package cn.wolfcode.luowowo.website.controller;


import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.util.SettokenUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {
    @Reference
    private IUserInfoService userInfoService;

    @RequestMapping("/get")
    @ResponseBody
    public Object get(Long id){
        return userInfoService.get(id);
    }


    //判断手机号是否存在
    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone){
        return  !userInfoService.checkPhone(phone);
    }


    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);
        return  new AjaxResult();
    }

    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone, String nickname,String password, String rpassword, String verifyCode){
        try {
            userInfoService.userRegist(phone, nickname, password, rpassword, verifyCode);
        }catch (LogicException e1) {
            e1.printStackTrace();
            return new AjaxResult(false,e1.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙~~~稍后试");
        }
        return  AjaxResult.SUCCESS;
    }

    //登录
    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(String username, String password,HttpServletResponse response){
        String token = userInfoService.userLogin(username, password);
        //将token设置进浏览器
        SettokenUtil.settoken(token, response);
        return AjaxResult.SUCCESS;
    }
}
